package com.gitee.wsl.unit

import com.gitee.wsl.mathematics.number.compareTo


interface UnitType<V:Number> {
    val baseUnit:V
}

interface UnitValue<V>{
    val value: V
}

abstract class BaseUnit<V:Number,TYPE:UnitType<V>>(
    override var value:V,
    val type:TYPE
):UnitValue<V>,Comparable<BaseUnit<V,TYPE>>{

    abstract fun toBase():V

     open fun format():String {
         return "$value ${type::class.qualifiedName}"
     }

    abstract fun convertTo( desType: TYPE): BaseUnit<V,TYPE>

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other !is BaseUnit<*, *>) return false

        return toBase() == other.toBase()
    }

    override fun hashCode(): Int {
        var result = value.hashCode()
        result = 31 * result + type.hashCode()
        return result
    }

    override fun toString(): String {
        return format()
    }

    override fun compareTo(other: BaseUnit<V, TYPE>): Int = toBase().compareTo(other.toBase())
}

abstract class SubBaseUnit<V:Number,TYPE:UnitType<V>,SUB>(value:V, type:TYPE, val bUnitValue:SUB?=null):BaseUnit<V,TYPE>(value, type){

}
